/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.*;
import javax.swing.tree.TreeSelectionModel;
import org.w3c.dom.*;
import groupe.App;

/**
 *
 * @author Ter Murga Victor
 */
public class ComposantsView extends JPanel/* implements ActionListener*/{
    private Image drawingImage;
    private NodeList listArbreDesComposantes;
    private Element structure;
    public Dimension dimension;
    public String total="STRUCTURE";
    DefaultMutableTreeNode n2 =new DefaultMutableTreeNode(total);
   
    public ComposantsView(Dimension ecran){
        super(new GridLayout(0,1));
        setBorder(new LineBorder(Color.BLACK, 1));
        
        //setPreferredSize(new Dimension((ecran.width-55)/3, (ecran.height-160)/2));
        setPreferredSize(new Dimension(480,145));
        //JScrollPane scroll = new JScrollPane();
        //scroll.getVerticalScrollBar().setUnitIncrement(10);
       // add(scroll,this);
        //setAutoscrolls(true);
        //JTextArea name = new JTextArea();
        //name.setBackground(Color.red);
        //name.setFont(Utility.font);
        //name.setc
        //name.setBounds(Utility.thickness, Utility.thickness, (ecran.width-55)/3-Utility.thickness*2, 20);
        //add(name);

    }
    public void ajouter(Element brique){
        //String nom = "EcoleEte";
        String nom = brique.getAttribute("nom");
        //int type = Integer.parseInt(brique.getAttribute("type"));
           //brique.getAttribute(nom);
       DefaultMutableTreeNode n1;

       // setToolTipText("brique "+nom);
       // this.brique = brique;
        //setLayout(null);
        //Recuperer la liste d'arbres des composantes
       listArbreDesComposantes = brique.getElementsByTagName("arbre_des_composantes");
        //Utility.ajouterTree(listArbreDesComposantes, nom, total);
        //add(Utility.createTree(listArbreDesComposantes, nom));
       n1= Utility.createTree(listArbreDesComposantes, nom);
     n2.add(n1)/*= Utility.ajouterTree(n1, nom, total)*/;
       //add(Utility.montrer(n1, nom));
       //JScrollPane scrollPane = Utility.createTree(listArbreDesComposantes, brique.getAttribute("nom"));
       
    }
    public void montrer(){
       //add(Utility.montrer(n2, total));
   JScrollPane scrollPane = new JScrollPane(Utility.montrer(n2, total));
        //JScrollPane scrollPane = Utility.createTree(listArbreDesComposantes, brique.getAttribute("nom"));
   scrollPane.setBorder(null);
        //scrollPane.set
        //scrollPane.setBounds(Utility.thickness, 22+Utility.thickness, (ecran.width-55)/3-Utility.thickness*2, (ecran.height-160)/2-22-Utility.thickness*2);
   add(scrollPane, BorderLayout.CENTER);
    }
    
    //private void createTree
    public void dessiner() {
        Dimension dim = new Dimension(0, 1);
        //System.out.println(this.getParent().getWidth());
        
        if (Utility.getCommentaire(structure)!=null){
            JLabel label = new JLabel();
            label.setText("<html>"+Utility.getCommentaire(structure));
            add(label);
            
            
            /*Font f = new Font("Arial", Font.PLAIN, 15);
            g.setFont(f);
            g.drawString("<html>"+Utility.getCommentaire(brique), 0, 17);
            dim.height++;*/
        }

        for (int i=0; i<listArbreDesComposantes.getLength();i++){
            //Recuperer l'arbre des composantes
            Element arbre = (Element) listArbreDesComposantes.item(i);

            // Recuperer la liste des composantes
            NodeList listComposant = arbre.getElementsByTagName("composante");
            //listComposantes = new String[listComposant.getLength()];
            DefaultMutableTreeNode tree = new DefaultMutableTreeNode(arbre.getAttribute("nom"));
            /*for (int j=0; j<listComposant.getLength(); j++){

                //Element composant = (Element) listComposant.item(j);
                Node composant = listComposant.item(j);

                DefaultMutableTreeNode tmp =Utility.dessinerComposante(null, composant, dim);
                tree.add(tmp);
            }*/
        }
        //setPreferredSize(new Dimension(dim.width,dim.height*21));
        //return dim;
        //setVisible(true);
    }


    public Graphics getImageGraphics() {
        if (drawingImage == null) {
            reset();
        }
        return drawingImage.getGraphics();
    }

    private void reset() {
        Dimension dim = getSize();
        drawingImage = this.createImage(dim.width, dim.height);
        //System.out.println("-->>dans reset, w= "+dim.width+" / h= "+dim.height+" / "+drawingImage);
        //Graphics g = drawingImage.getGraphics();
        /*Graphics g = drawingImage.getGraphics();
	//Color c = g.getColor();
	g.setColor(Color.white);
	g.fillRect(0,0,dim.width, dim.height);
	//g.setColor(c);*/
    }

    @Override
    public void paintComponent(Graphics g) {
        if (drawingImage == null) {
            reset();
        }
        g.drawImage(drawingImage, 0, 0, null);
        //dessiner(g);
    }

        
    
    public void drawIt() {
        Graphics g = getGraphics();
        g.drawImage(drawingImage, 0, 0, null);
        //return dess(g);
        /*JLabel label = new JLabel("Visualise : ");
        add(label);

        JComboBox colorList = new JComboBox(listComposantes);
        add(colorList);*/


        //colorList.addActionListener();
        
        //dessiner(g);
    }

    /*public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
		int n = cb.getSelectedIndex();
                Graphics g = getGraphics();
                reset();
                g.setColor(Color.white);
                g.fillRect(0,0,500  , 500);
                
                //Graphics g = getGraphics();

                Dimension dim = new Dimension(0, 1);
                Node composant = ((Element)listArbreDesComposantes.item(0)).getElementsByTagName("composante").item(n-1);
                dimension=Utility.dessiner(g, composant, dim);
                setVisible(true);

                System.out.println(listComposantes[n]);
				//courante.setColor(n);


    }*/

}
